home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / cli / mcomms_1_4.lha / Src / time.c < prev    next >
C/C++ Source or Header  |  1995-08-31  |  2KB  |  102 lines

  1. /*
  2. **    time.c - measure command execution time
  3. **    Copyright ⌐ 1994-95 Michael Letowski
  4. */
  5.  
  6. #define __USE_SYSBASE
  7.  
  8. #include <exec/types.h>
  9. #include <exec/execbase.h>
  10. #include <dos/dos.h>
  11. #include <dos/rdargs.h>
  12. #include <dos/dostags.h>
  13. #include <devices/timer.h>
  14. #include <utility/tagitem.h>
  15. #include <support/types.h>
  16. #include <support/dos.h>
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20. #include <proto/timer.h>
  21.  
  22. #include "time.rev.h"
  23.  
  24.  
  25. #define DOS_NAME        "dos.library"
  26. #define DOS_VERN        37
  27. #define TIMER_NAME    "timer.device"
  28.  
  29. STATIC CONST TEXT VersionString[]=
  30.     VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);
  31.  
  32. #define TEMPLATE        "NOHEAD/S,COMMAND/F/A"
  33.  
  34. struct Options
  35. {
  36.     LONG   opt_NoHeader;                                                    /* NOHEAD/S */
  37.     STRPTR opt_Command;                                                        /* COMMAND/F/A */
  38. };    /* Options */
  39.  
  40. LONG Time(VOID)
  41. {
  42.     STATIC CONST struct TagItem SysTags[]=
  43.     {
  44.         {SYS_UserShell,    TRUE},
  45.         {TAG_DONE,            0}
  46.     };    /* SysTags */
  47.  
  48.     struct ExecBase *SysBase=*((struct ExecBase **)4);
  49.     struct DosLibrary *DOSBase;
  50.     struct Library *TimerBase;
  51.  
  52.     struct Options Opts;
  53.     struct timeval TV1,TV2;
  54.     struct RDArgs *Args;
  55.     struct MsgPort *Port;
  56.     struct timerequest *TR;
  57.     LONG RC=RETURN_FAIL;
  58.  
  59.     /* Open libraries */
  60.     unless(DOSBase=(struct DosLibrary *)OpenLibrary(DOS_NAME,DOS_VERN))
  61.         throw2(SetResult2(ERROR_INVALID_RESIDENT_LIBRARY),    NO_DOS);
  62.  
  63.     /* Read args */
  64.     clear(&Opts);
  65.     unless(Args=ReadArgs(TEMPLATE,(LONG *)&Opts,NULL))
  66.         throw2(PrintFault(IoErr(),PROG_NAME),    NO_ARGS);
  67.  
  68.     /* Do timing */
  69.     if(Port=CreateMsgPort())
  70.     {
  71.         if(TR=CreateIORequest(Port,sizeof(*TR)))
  72.         {
  73.             if(OpenDevice(TIMER_NAME,UNIT_VBLANK,(struct IORequest *)TR,0)==0)
  74.             {
  75.                 TimerBase=(struct Library *)TR->tr_node.io_Device;
  76.  
  77.                 GetSysTime(&TV1);
  78.                 RC=SystemTagList(Opts.opt_Command,SysTags) ? RETURN_ERROR : RETURN_OK;
  79.                 GetSysTime(&TV2);
  80.                 SubTime(&TV2,&TV1);
  81.  
  82.                 /* Print results */
  83.                 if(RC==RETURN_ERROR)                                        /* Error during execution */
  84.                     PrintFault(IoErr(),PROG_NAME);
  85.                 if(Opts.opt_NoHeader)    VPrintf("%lu.%06lu\n",&TV2);
  86.                 else                                    VPrintf("Execution time: %lu.%06lu s.\n",&TV2);
  87.  
  88.                 CloseDevice((struct IORequest *)TR);
  89.             }
  90.             DeleteIORequest(TR);
  91.         }
  92.         DeleteMsgPort(Port);
  93.     }
  94.  
  95.     if(RC==RETURN_FAIL)                                                /* Couldn't use timer */
  96.         CauseIoErr(ERROR_NO_FREE_STORE,PROG_NAME);
  97.  
  98.     catch(NO_ARGS,    FreeArgs(Args));
  99.     catch(NO_DOS,        CloseLibrary((struct Library *)DOSBase));
  100.     return(RC);
  101. }    /* Time */
  102.